package warmachineroster.service.validation.calculators;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import warmachinesystem.BaseUnit;
import warmachinesystem.TierLevelType;

public class TierStatus {

	
	private List<BaseUnit> offendingUnits = new ArrayList<BaseUnit>();
	
	private Map<BaseUnit, Integer> freeUnits = new HashMap<BaseUnit, Integer>();
	
	private Map<BaseUnit, Integer> unitCostDelta = new HashMap<BaseUnit, Integer>();
	
	private Map<BaseUnit, String> unitFaDelta = new HashMap<BaseUnit, String>();

	private List<String> ingameRules = new ArrayList<String>();
	
	private TierLevelType currentTierLevel = TierLevelType.NO_TIER;
	
	public List<BaseUnit> getOffendingUnits() {
		return new ArrayList<BaseUnit>(offendingUnits);
	}

	public Map<BaseUnit, Integer> getFreeUnits() {
		return new HashMap<BaseUnit, Integer>(freeUnits);
	}

	public Map<BaseUnit, Integer> getUnitCostDelta() {
		return new HashMap<BaseUnit, Integer>(unitCostDelta);
	}

	public Map<BaseUnit, String> getUnitFaDelta() {
		return new HashMap<BaseUnit, String>(unitFaDelta);
	}

	public List<String> getIngameRules() {
		return new ArrayList<String>(ingameRules);
	}

	public TierLevelType getCurrentTierLevel() {
		return currentTierLevel;
	}
	
	protected void addOffendingUnit(BaseUnit offendingUnit) {
		this.offendingUnits.add(offendingUnit);
	}

	protected void addFreeUnit(BaseUnit unit, Integer numberOfFreeUnits) {
		this.freeUnits.put(unit, numberOfFreeUnits);
	}

	protected void addFreeUnits(List<BaseUnit> unitList, Integer numberOfUnits){
		for(BaseUnit bu: unitList){
			addFreeUnit(bu, numberOfUnits);
		}
	}
	
	protected void addUnitCostDelta(BaseUnit unit, Integer unitCostDelta) {
		this.unitCostDelta.put(unit, unitCostDelta);
	}
	
	protected void addUnitCostDelta(List<BaseUnit> unitList, Integer unitCostDelta){
		for(BaseUnit bu: unitList){
			addUnitCostDelta(bu, unitCostDelta);
		}
	}

	protected void addUnitFaDelta(BaseUnit unit , String unitFaDelta) {
		String actualFa = this.unitFaDelta.get(unit);
		if ("U".equals(actualFa)){
			return;
		} else if ("U".equals(unitFaDelta)){
			this.unitFaDelta.put(unit, unitFaDelta);
			return;
		}
		int intFaActual = 0;
		try{
			intFaActual = Integer.parseInt(actualFa);
		}catch (NumberFormatException e){}
		
		int intUnitFaDelta = 0;
		try {
			intUnitFaDelta = Integer.parseInt(unitFaDelta);
		} catch (NumberFormatException e) {}
		
		this.unitFaDelta.put(unit, String.valueOf(intFaActual + intUnitFaDelta));
	}
	
	protected void addUnitFaDelta(List<BaseUnit>unitList, String unitFaDelta){
		for(BaseUnit bu: unitList){
			addUnitFaDelta(bu, unitFaDelta);
		}
	}
	
	
	protected void addIngameRule(String rule) {
		this.ingameRules.add(rule);
	}
	
	protected void setCurrentTierLevel(TierLevelType currentTierLevel) {
		this.currentTierLevel = currentTierLevel;
	}
}
